package com.haozi.ehub.core.security;

import com.haozi.ehub.core.convert.JwtUserConvert;
import com.haozi.ehub.core.enums.ExceptionEnum;
import com.haozi.ehub.core.exception.UsernameOrPasswordErrorException;
import com.haozi.ehub.core.model.User;
import com.haozi.ehub.core.service.AdminInfoService;
import com.haozi.ehub.core.service.UserInfoService;
import com.haozi.ehub.core.util.CodeUtils;
import com.haozi.ehub.core.util.RSAEncoderUtils;
import com.haozi.ehub.core.util.RedisUtils;
import com.haozi.ehub.entity.entities.AdminInfo;
import com.haozi.ehub.entity.entities.JwtUserInfo;
import com.haozi.ehub.entity.entities.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 用户权限验证
 * @author ：terry
 * @date ：Created in 2020/6/15 9:42
 */
@Service
@SuppressWarnings(value = "all")
public class UserDetailsServiceImpl implements UserDetailsService {

    /**
     * redis工具类
     * @author     ： terry
     * @date       ： Created in 2020/7/6 10:40
     */
    private final RedisUtils redisUtil;
    /**
     * 用户service
     * @author     ： terry
     * @date       ： Created in 2020/7/3 10:41
     */
    private final UserInfoService userInfoService;
    /**
     * 管理员service
     * @author     ： terry
     * @date       ： Created in 2020/7/3 10:41
     */
    private final AdminInfoService adminInfoService;
    @Autowired
    public UserDetailsServiceImpl(RedisUtils redisUtil,UserInfoService userInfoService,AdminInfoService adminInfoService){
        this.redisUtil = redisUtil;
        this.userInfoService = userInfoService;
        this.adminInfoService = adminInfoService;
    }


    @Override
    public UserDetails loadUserByUsername(String s) throws AuthenticationException {
        final String loginValidationFailMsg = ExceptionEnum.LOGIN_VALIDATION_FAIL.getEnmsg();
        final String userNameNullMsg = ExceptionEnum.USER_NAME_IS_NULL.getEnmsg();
        if(StringUtils.isEmpty(s)){
            throw new UsernameOrPasswordErrorException(userNameNullMsg);
        }
        //由于http请求会把字符串中的“+”替换成“ ”，所以先做一次字符替换
        if(StringUtils.contains(s, CodeUtils.SPACE)){
            s = StringUtils.replace(s, CodeUtils.SPACE, CodeUtils.PLUS_SIGN);
        }
        //获得通过私钥解密后的字符串
        final String decrypt = RSAEncoderUtils.decrypt(s);
        String[] splitName = new String[]{};
        //根据下划线分割用户名
        if(StringUtils.contains(decrypt,CodeUtils.UNDERLINE)){
            splitName = StringUtils.split(decrypt, CodeUtils.UNDERLINE);
        }
        if(splitName.length <= 1){
            throw new UsernameOrPasswordErrorException(userNameNullMsg);
        }
        if(StringUtils.isEmpty(splitName[0]) || StringUtils.isEmpty(splitName[1])){
            throw new UsernameOrPasswordErrorException(userNameNullMsg);
        }

        //新建用于验证的用户对象
        JwtUserInfo jwtUserInfo = new JwtUserInfo();
        if(CodeUtils.ADMIN_PREFIX.equals(splitName[0])){
            AdminInfo adminInfo = adminInfoService.selectAdminByName(splitName[1]);
            if(adminInfo != null){
                jwtUserInfo = JwtUserConvert.convert2JwtUser(adminInfo);
            }
        }else if(CodeUtils.USER_PREFIX.equals(splitName[0])){
            UserInfo userInfo = userInfoService.getById(splitName[1]);
            if(userInfo != null){
                jwtUserInfo = JwtUserConvert.convert2JwtUser(userInfo);
            }
        }else{
            throw new UsernameOrPasswordErrorException(userNameNullMsg);
        }
        if(jwtUserInfo == null){
            throw new UsernameNotFoundException(loginValidationFailMsg);
        }
        return new User(jwtUserInfo);
    }
}
